package com.dom.domweb.modular.service;

import com.dom.common.enums.MenuTypeEnum;
import com.dom.common.utils.BeanUtil;
import com.dom.domweb.dao.entity.TMenuInfo;
import com.dom.domweb.dao.service.ITMenuInfoService;
import com.dom.domweb.dao.service.ITRolePrivRelService;
import com.dom.domweb.model.MenuTree;
import com.dom.domweb.modular.shiro.model.ShiroUser;
import com.xiaoleilu.hutool.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 首页处理service
 * @Author xllei
 * @Date 2018/11/19 10:43 AM
 * @Version 1.0
 **/
@Service
public class IndexService {
    @Autowired
    private ITRolePrivRelService rolePrivRelService;
    @Autowired
    private ITMenuInfoService menuInfoService;

    private List<TMenuInfo> getRoleMenu(Integer memCode, List<Integer> roles){
        return menuInfoService.getMenuList(memCode, roles);
    }

    public MenuTree loadUserMenu(ShiroUser user) {
        List<TMenuInfo> menuInfos = getRoleMenu(user.getMemCode(), user.getRoles());
        return buildMenuTree(menuInfos);
    }

    public MenuTree buildMenuTree(List<TMenuInfo> menuInfos){
        Map<Integer, List<TMenuInfo>> mapMenu = menuInfos.stream().collect(Collectors.groupingBy(TMenuInfo::getMenuType));
        List<TMenuInfo> menu1 = mapMenu.get(MenuTypeEnum.MENU_LEVEL_FIRST.getCode());
        if(CollectionUtil.isEmpty(menu1)){
            return null;
        }

        //将二级菜单按照父级菜单类型分组
        List<TMenuInfo> menu2 = mapMenu.get(MenuTypeEnum.MENU_LEVEL_SECOND.getCode());
        Map<Integer, List<TMenuInfo>> mapInduction = menu2.stream().collect(Collectors.groupingBy(TMenuInfo::getFatherMenuId));

        //定义菜单根节点
        MenuTree rootNode = new MenuTree();
        rootNode.setId(0);
        rootNode.setMenuType(0);
        rootNode.setMenuName("菜单根节点");

        List<MenuTree> leafNodes = new ArrayList<>();
        menu1.forEach(firstMenu ->{
            MenuTree tree = new MenuTree();
            BeanUtil.copyPropertiesIgnoreNull(firstMenu, tree);
            List<TMenuInfo> childMenus = mapInduction.get(firstMenu.getId());
            List<MenuTree> secondMenus = new ArrayList<>();
            if(CollectionUtil.isNotEmpty(childMenus)){
                childMenus.forEach( menu ->{
                    MenuTree menuTree = new MenuTree();
                    BeanUtil.copyPropertiesIgnoreNull(menu, menuTree);
                    secondMenus.add(menuTree);
                });
                secondMenus.sort(Comparator.comparing(MenuTree::getId));
            }

            tree.setChildNodes(secondMenus);
            leafNodes.add(tree);
        });

        leafNodes.sort(Comparator.comparing(MenuTree::getId));
        rootNode.setChildNodes(leafNodes);
        return rootNode;
    }

    private List<TMenuInfo> getRoleMenuList(Integer memCode, List<Integer> roles){
        return menuInfoService.getRoleMenuList(memCode, roles);
    }

    public MenuTree loadNavMenu(ShiroUser user){
        List<TMenuInfo> menuInfos = getRoleMenuList(user.getMemCode(), user.getRoles());

        return buildMenuTree(menuInfos);
    }
}
